#include <QPainter>
#include <QLabel>
#include <QDebug>
#include <QPropertyAnimation>
#include "gamescene.h"
#include "gameconfig.h"
#include "gamepushbutton.h"
#include "gamecontroller.h"
#include "gamedatasource.h"
#include "gamedatasource.h"
GameScene::GameScene(int level, QWidget *parent) : QMainWindow(parent)
  ,levelId(level)
{
    this->setStyleSheet("QPushButton { border: 0px; } QPushButton:disabled { color: black; }");
    this->setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    this->setWindowTitle(tr("游戏界面"));
    this->setWindowIcon(QIcon(GAME_ICON));
    //返回上一个界面按钮
    GamePushButton *backBtn = new GamePushButton(BTN_BACK_NORMAL);
    backBtn->setParent(this);
    backBtn->move(GAME_WIDTH - backBtn->width(), GAME_HEIGHT - backBtn->height());
    //游戏胜利标志
    m_lblWinGame = new QLabel(this);
    QPixmap winPixmap(IMG_LEVEL_COMPLETE);
    m_lblWinGame->setPixmap(winPixmap);
    m_lblWinGame->setGeometry(0,0,winPixmap.width(),winPixmap.height());
    m_lblWinGame->move((GAME_WIDTH-m_lblWinGame->width())/2,-100);

    //游戏关卡标志设置
    QLabel *Level_marker = new QLabel(this);
    Level_marker->setText(QString("LEVEL-%1").arg(levelId));
    Level_marker->setGeometry(10,GAME_HEIGHT - 80,200,50);
    QFont levelFont;
    levelFont.setFamily("黑体");
    levelFont.setBold(true);
    levelFont.setPointSize(20);
    Level_marker->setFont(levelFont);

    //返回按钮的槽函数
    connect(backBtn, &GamePushButton::clicked, this, [=](){
        GameController::getInstance()->deleteGameScene();
        GameController::getInstance()->playBackSound();
        GameController::getInstance()->showLevelScene();

    });
    //游戏设置
    GameDataSource gameDataInfo;
    for (int i = 0; i < 4; i++ ) {
        for (int j = 0; j <4; j++ ) {
            m_gameInfoArray[i][j] = gameDataInfo.mData[levelId][i][j];
            //游戏背景
            QPixmap pix = QPixmap(IMG_GAME_BACKGROUND);
            QLabel *gameLabel = new QLabel(this);
            gameLabel->setGeometry(95 + i*50, 200 + j*50,pix.width(),pix.height());
            gameLabel->setPixmap(pix);
            //金币初始化
            QString strCoin = "";
            if(m_gameInfoArray[i][j] == 1){
                strCoin = ":/res/Coin0001.png";
            } else {
                strCoin = ":/res/Coin0008.png";
            }

            m_btnCoin[i][j] = new GameCoin(strCoin,gameLabel);
            //把金币的位置和正反信息传入金币类
            m_btnCoin[i][j]->setPosX(i);
            m_btnCoin[i][j]->setPosY(j);
            m_btnCoin[i][j]->setFlag(m_gameInfoArray[i][j]);
            //每个金币的点击事件
            connect(m_btnCoin[i][j], &QPushButton::clicked, this, [=](){
              m_gameInfoArray[i][j] =  m_gameInfoArray[i][j] == 1?0:1;
              m_btnCoin[i][j]->changeFlag();
              GameController::getInstance()->playFlipCoinSound();
              QTimer::singleShot(30,this,[=](){
                  int xIndex = m_btnCoin[i][j]->posX();
                  int yIndex = m_btnCoin[i][j]->posY();
                  //判断上面是否有金币
                  if (xIndex > 0)
                  {
                      m_gameInfoArray[xIndex - 1][yIndex] = m_gameInfoArray[xIndex - 1][yIndex] == 1 ? 0 : 1;
                      m_btnCoin[xIndex - 1][yIndex]->changeFlag();
                  }
                  //判断下面是否有金币
                  if (xIndex < 3)
                  {
                      m_gameInfoArray[xIndex + 1][yIndex] = m_gameInfoArray[xIndex + 1][yIndex] == 1 ? 0 : 1;
                      m_btnCoin[xIndex + 1][yIndex]->changeFlag();
                  }
                  //判断左边是否有金币
                  if (yIndex > 0)
                  {
                      m_gameInfoArray[xIndex][yIndex - 1] = m_gameInfoArray[xIndex][yIndex - 1] == 1 ? 0 : 1;
                      m_btnCoin[xIndex][yIndex - 1]->changeFlag();
                  }
                  //判断右边是否有金币
                  if (yIndex < 3)
                  {
                      m_gameInfoArray[xIndex][yIndex + 1] = m_gameInfoArray[xIndex][yIndex + 1] == 1 ? 0 : 1;
                      m_btnCoin[xIndex][yIndex + 1]->changeFlag();
                  }
                  //判断是否获胜
                  bool gaemisWin = true;
                  for (int m = 0; m < 4 ; m++ ) {
                      for (int n = 0; n < 4 ; n++ ) {
                          if (m_gameInfoArray[m][n] == 0)
                          {
                              gaemisWin = false;
                              break;
                          }
                      }
                  }
                  if (gaemisWin == true)
                  {
                       GameController::getInstance()->playWinSound();


                       //胜利动画
                       //胜利动画
                       QPropertyAnimation *winAnimation=new QPropertyAnimation(m_lblWinGame,"geometry");
                       winAnimation->setDuration(1000);
                       winAnimation->setStartValue(QRect(m_lblWinGame->x(),m_lblWinGame->y()
                                                                                  ,m_lblWinGame->width(),m_lblWinGame->height()));
                       winAnimation->setEndValue(QRect(m_lblWinGame->x(),40
                                                                                ,m_lblWinGame->width(),m_lblWinGame->height()));
                       winAnimation->setEasingCurve(QEasingCurve::OutBounce);
                       winAnimation->start();
                       for(int m = 0; m < 4;m++){
                           for(int n = 0; n < 4;n++){
                               // m_btnCoin[m][n]->setIsWin(true);
                               QTimer::singleShot(500,this,[=](){
                                   m_btnCoin[m][n]->setEnabled(false);

                               });
                           }
                       }
                  }

              });


            });

        }

    }


}

void GameScene::paintEvent(QPaintEvent *event)
{


    QPainter gamePainter(this);
    QPixmap gamePixmap;
    bool res = gamePixmap.load(IMG_BG_GAMESCENE);
    if (res)
    {
        gamePainter.drawPixmap(0, 0, gamePixmap);
    }
    gamePixmap.load(IMG_GAME_TITLE);
    gamePainter.drawPixmap((this->width() - gamePixmap.width())/2, 30, gamePixmap);
}





